# -*- coding: utf-8 -*-
import numpy as np
import torch
from torch.autograd import Variable
import torch.nn as nn
import torch.optim as optim
import matplotlib.pylab as plt

x_train = np.array([[3.3], [4.4], [5.5], [6.71], [6.93], [4.168],
                    [9.779], [6.182], [7.59], [2.167], [7.042],
                    [10.791], [5.313], [7.997], [3.1]], dtype=np.float32)

y_train = np.array([[1.7], [2.76], [2.09], [3.19], [1.694], [1.573],
                    [3.366], [2.596], [2.53], [1.221], [2.827],
                    [3.465], [1.65], [2.904], [1.3]], dtype=np.float32)
x_train = torch.from_numpy(x_train)
y_train = torch.from_numpy(y_train)

class LinearRegression(nn.Module):
    def __init__(self):
        super(LinearRegression, self).__init__()
        # 定义每层使用什么形式
        self.linear = nn.Linear(1, 1)
    def forward(self, x):
        out = self.linear(x)
        return out

model = LinearRegression()
# 训练网络
criterion = nn.MSELoss()  # 预测值和真实值的误差计算公式 (均方差)
optimizer = optim.Adam(model.parameters(), lr=1e-4)  # 传入 model 的所有参数, 学习率，优化

num_epochs = 1000
for epoch in range(num_epochs):
    inputs = Variable(x_train, requires_grad=True)
    target = Variable(y_train, requires_grad=True)
    # forward
    out = model(inputs) # 前向传播
    loss = criterion(out, target)  #一个损失函数接受一对(output, target)作为输入(output为网络的输出,target为实际值),计算一个值来估计网络的输出和目标值相差多少
    # backward
    optimizer.zero_grad()#梯度归零
    loss.backward()      #反向传播,调用loss.backward(),整个图关于损失被求导,图中所有变量将拥有.grad变量来累计他们的梯度
    optimizer.step()     #更新参数
    if (epoch) % 20 == 0:
        print('Epoch[{}/{}], loss: {:.6f}'.format(epoch, num_epochs, loss.data[0]))

model.eval()
predict = model(Variable(x_train))
predict = predict.data.numpy()
plt.plot(x_train.numpy(), y_train.numpy(),'ro', label='Original data')
plt.plot(x_train.numpy(), predict, label = 'Fitting Line')

# 显示图例
plt.legend()
plt.show()

# 保存模型
torch.save(model.state_dict(), './linear.pth')